package com.example.botdemo02;

import com.example.botdemo02.client.BotClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Timer;
import java.util.TimerTask;

@SpringBootApplication
@Slf4j
public class Botdemo02Application implements CommandLineRunner {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Botdemo02Application.class, args);
        BotClient.setApplicationContext(context);
    }

    @Override
    public void run(String... args) throws Exception {
        BotClient.connect("ws://127.0.0.1:9090");
        // 断线重连
        Timer t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (!BotClient.isOpen || BotClient.instance == null) {
                    BotClient.connect("ws://127.0.0.1:9090");
                    log.error("正在断线重连！");
                }
            }
        }, 1000, 5000);
    }
}
